package com.yami.shop.api.controller;

import com.yami.shop.bean.meituan.CheckResponse;
import com.yami.shop.bean.meituan.ShopStatusResponse;
import com.yami.shop.bean.model.MeituanShopStatus;
import com.yami.shop.service.MeituanShopStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/shop")
public class ShopStatusController {
    @Autowired
    private MeituanShopStatusService meituanShopStatusService;

    //查询数据库有没有该店的信息，如果有数据，对数据进行修改。没有数据，则添加数据进去
    @PostMapping("/getShopStatus")
    public ResponseEntity<Map> getShopStatus( ShopStatusResponse shopStatusResponse){
        String shopId = shopStatusResponse.getShopId();
        if(shopStatusResponse!=null) {
            if (null != shopId && !"".equals(shopId)) {
                MeituanShopStatus meituanShopStatus = meituanShopStatusService.getMeituanShopByShopId(Long.valueOf(shopId));
                if (meituanShopStatus != null) {
                    meituanShopStatus.setStatus(shopStatusResponse.getStatus());
                    meituanShopStatus.setRejectMessage(shopStatusResponse.getRejectMessage());
                    meituanShopStatusService.saveOrUpdate(meituanShopStatus);
                } else {
                    MeituanShopStatus shopStatus = new MeituanShopStatus();
                    shopStatus.setShopId(Long.valueOf(shopStatusResponse.getShopId()));
                    shopStatus.setShopName(shopStatusResponse.getShopName());
                    shopStatus.setStatus(shopStatusResponse.getStatus());
                    shopStatus.setRejectMessage(shopStatusResponse.getRejectMessage());
                    meituanShopStatusService.save(shopStatus);
                }
            }
        }
        Map map =new HashMap();
        map.put("code","0");
        return  ResponseEntity.ok(map);
    }
}
